Skip to content

Fix integer overflow in file read path - #252

Merged
jeromekelleher merged 2 commits into
tskit-dev:mainfrom
jeromekelleher:fix-security-vuln
Jun 26, 2026
Merged

jeromekelleher merged 2 commits into
tskit-dev:mainfrom
jeromekelleher:fix-security-vuln

Conversation

@jeromekelleher

Copy link
Copy Markdown
Member

Harden kastore_read_descriptors against crafted files: the bounds checks key_start + key_len and array_start + array_len * type_size were computed in 64-bit arithmetic on untrusted values and could wrap around past file_size. A file declaring e.g. array_len = 2^61 of KAS_UINT64 passed all validation (including the structural checks, which accumulate the same wrapping product) but caused kastore_read_item to under-allocate while reporting a huge array_len, leading to an out-of-bounds read in the caller. The checks are now written with subtraction and division so they cannot overflow.

Also reject zero-length keys on read (matching the write-side invariant) and replace the reachable assert(size > offset) in kastore_read_file with a KAS_ERR_BAD_FILE_FORMAT return, and guard the (long) cast of array_start in kastore_read_item's fseek against truncation on LLP64 platforms.

Add tests covering the array_len and key_len overflow cases, zero-length keys, and a positive control.

Closes #251

@petrelharp petrelharp left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code LGTM. Looks like this needs the updated actions (I didn't do those here).

Harden kastore_read_descriptors against crafted files: the bounds
checks key_start + key_len and array_start + array_len * type_size
were computed in 64-bit arithmetic on untrusted values and could wrap
around past file_size. A file declaring e.g. array_len = 2^61 of
KAS_UINT64 passed all validation (including the structural checks,
which accumulate the same wrapping product) but caused
kastore_read_item to under-allocate while reporting a huge array_len,
leading to an out-of-bounds read in the caller. The checks are now
written with subtraction and division so they cannot overflow.

Also reject zero-length keys on read (matching the write-side
invariant) and replace the reachable assert(size > offset) in
kastore_read_file with a KAS_ERR_BAD_FILE_FORMAT return, and guard the
(long) cast of array_start in kastore_read_item's fseek against
truncation on LLP64 platforms.

Add tests covering the array_len and key_len overflow cases, zero-length
keys, and a positive control.

Add changelog notes for read-path overflow security fix

Document the integer-overflow vulnerability in the file read path
(crafted descriptors leading to an under-allocated buffer and an
out-of-bounds read) in both the C and Python changelogs.
@codecov

codecov Bot commented Jun 26, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 91.40%. Comparing base (5ee8549) to head (4376306).
⚠️ Report is 2 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main     #252      +/-   ##
==========================================
+ Coverage   91.30%   91.40%   +0.09%     
==========================================
  Files           8        8              
  Lines        1346     1350       +4     
  Branches      194      196       +2     
==========================================
+ Hits         1229     1234       +5     
  Misses        106      106              
+ Partials       11       10       -1     
Flag Coverage Δ
C 98.45% <100.00%> (+0.16%) ⬆️
c-python 60.60% <ø> (ø)
python-tests 99.31% <ø> (ø)

Flags with carried forward coverage won't be shown. Click here to find out more.

Components Coverage Δ
Python API 99.54% <ø> (ø)
Python C interface 60.60% <ø> (ø)
C library 98.45% <100.00%> (+0.16%) ⬆️
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Two error conditions added in the read-path security fix were not
exercised by any test:

- The size <= offset geometry guard in kastore_read_file was dead code:
  kastore_read_descriptors already rejects zero-length keys, which
  guarantees items[0].array_start > offset. Remove the redundant guard
  and document that the subtraction cannot underflow.

- The LLP64 fseek-truncation guard in kastore_read_item (array_start >
  LONG_MAX - file_offset) was unreachable from a single-item file. Add
  test_array_start_seek_overflow, a crafted two-item file whose second
  array starts beyond LONG_MAX. It opens in lazy mode (only the keys are
  read) and fetching the second key reaches kastore_read_item, which now
  returns KAS_ERR_IO instead of seeking to a truncated offset.
@jeromekelleher
jeromekelleher merged commit de3eb00 into tskit-dev:main Jun 26, 2026
11 checks passed
@jeromekelleher
jeromekelleher deleted the fix-security-vuln branch June 26, 2026 10:39
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Potential security vulnerability

2 participants